package com.OnTimeTeam.OnTime;


import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;
import android.widget.Toast;



public class TransitOptions extends Activity {
		
		ListView list;
		BinderData adapter = null;
		
		TextView startName;
		TextView endName;	
		
		Button buttonOk;
		Button buttonCancel;
		
		private static int  chosenPosition = 0;
		TransitRoadsQuery roadsQ;
		
		@Override
		protected void onCreate(Bundle savedInstanceState) {
			// TODO Auto-generated method stub
			super.onCreate(savedInstanceState);
			setContentView(R.layout.transit_options);		
			Intent i = getIntent();	
			roadsQ = (TransitRoadsQuery) i.getSerializableExtra("roadsQ");
			
		    adapter = new BinderData(this, roadsQ.getTracks(), this.getBaseContext());
			list = (ListView) findViewById(R.id.list);
			
			startName = (TextView)findViewById(R.id.tvFrom);
	        endName = (TextView)findViewById(R.id.tvTo);
			 
	        startName.setText(roadsQ.getTracks().get(0).getStartPlaceFullName());
	        endName.setText(roadsQ.getTracks().get(0).getEndPlaceFullName());
			
	        buttonOk = (Button)findViewById(R.id.bSave);
	        buttonCancel = (Button)findViewById(R.id.bCancel);
	        
	        
	       // Toast.makeText(getApplicationContext(), getApplicationContext().getResources().getString(R.string.longClickRoute), Toast.LENGTH_LONG).show();
	        LayoutInflater inflater = (LayoutInflater) TransitOptions.this.getSystemService(LAYOUT_INFLATER_SERVICE);
	        View toastView = inflater.inflate(R.layout.costum_toast,null);				       
	        Toast routeToast = new Toast(TransitOptions.this);	        
	        
	        TextView textMessage = (TextView)toastView.findViewById(R.id.addressConfirmation);
	        textMessage.setText(getApplicationContext().getResources().getString(R.string.longClickRoute));	        
	        
	        routeToast.setView(toastView);
	        routeToast.setGravity(Gravity.BOTTOM|Gravity.CENTER, 0, 150);
	        routeToast.setDuration(Toast.LENGTH_LONG);
	        
			routeToast.show();
			
	        Log.i("BEFORE", "<<------------- Before SetAdapter-------------->>");

			list.setAdapter(adapter);

			Log.i("AFTER", "<<------------- After SetAdapter-------------->>");
			

		    list.setOnItemClickListener(new OnItemClickListener() {

				public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

				
					chosenPosition = position;
					BinderData.setSelectItem(chosenPosition);
					adapter.notifyDataSetChanged();
					
				}
					});
		    
		   
		    buttonOk.setOnClickListener(new Button.OnClickListener(){
		    	
		    	public void onClick(View v){
		    	
		    		roadsQ.setChosenTrackIndex(chosenPosition);
		        	Intent returnIntent = new Intent();
		        	returnIntent.putExtra("roadsQ",roadsQ);
		        	setResult(RESULT_OK,returnIntent);     
		        	finish();
					
		    	}
		    });
		    
		    buttonCancel.setOnClickListener(new Button.OnClickListener(){
		    	
		    	public void onClick(View v){
		    	
		    		onBackPressed();
		    		
		    	}
		    });
		    
		    list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
		        
		        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
		        	
		        	chosenPosition = position;
					BinderData.setSelectItem(chosenPosition);
					adapter.notifyDataSetChanged();
					
					// start the sample activity
					//!!!!!Need to change to start activity for results.. then if pressed ok on detailed track
					//will set okButton as clicked...
					Intent i = new Intent(TransitOptions.this, DetailedTrack.class);
					i.putExtra("position", chosenPosition);	
					i.putExtra("track", roadsQ.getTracks().get(chosenPosition));
					startActivityForResult(i,1);
					
				//	startActivity(i); 
		        	return true;
		        }
		    });


    
	              
		} 
		

		 
	    @Override
		protected void onDestroy() {
			// TODO Auto-generated method stub
			super.onDestroy();

		}

		@Override
		protected void onPause() {
			// TODO Auto-generated method stub
			super.onPause();

		}

		@Override
		protected void onResume() {
			// TODO Auto-generated method stub
			super.onResume();

		}
		
		public void onBackPressed() {

		  //  super.onBackPressed();
		   
			//Need to change this!!!! now the app doesn't crash on backPressed,
			//it sets the first suggested route to be the chosen one. 
			roadsQ.setChosenTrackIndex(0);
        	Intent returnIntent = new Intent();
        	returnIntent.putExtra("roadsQ",roadsQ);
        	setResult(RESULT_CANCELED,returnIntent);    
        	finish();
        	
		}
		

		protected void onActivityResult(int requestCode, int resultCode, Intent data) {
			
		      if (requestCode == 1) 
		    	  
		    	  if (resultCode == RESULT_OK) {
		        	  
		    		int chosenPosition = data.getIntExtra("position", 0);
		    		roadsQ.setChosenTrackIndex(chosenPosition);
		    		Intent returnIntent = new Intent();
		    		returnIntent.putExtra("roadsQ",roadsQ);
		    		returnIntent.putExtra("position",chosenPosition);
		        	setResult(RESULT_OK,returnIntent);     
		        	finish();
					
		    		
		    	  }
		    	  //RESULT_CANCEL - Do Nothing
		      
		      
	    }
		
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_transit_options, menu);
		return true;
	}
	
	

}
